/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.tnmk.worktime.service.action;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author tnmkhoi
 */
public abstract class ScheduledAction implements Runnable{
  private int count;
  private long repeatPeriod;
  private ScheduledExecutorService scheduler;
  public int inCreaseTime(){
    count++;
    return count;
  }
  public ScheduledAction(long repeatPeriod){
    this.repeatPeriod = repeatPeriod;
  }
  /**
   * This method does not reset counter (times)
   * @param period 
   */
  public void resetSchedule(long period){
    this.stop();
    this.repeatPeriod = period;
    this.start();
  }
  public void start(){
    scheduler = Executors.newScheduledThreadPool(1);
    scheduler.scheduleAtFixedRate(this, repeatPeriod, repeatPeriod, TimeUnit.SECONDS);    
  }
  public void stop(){
    scheduler.shutdown();
  }

  /**
   * @return the count
   */
  public int getCount() {
    return count;
  }

  /**
   * @param count the count to set
   */
  public void setCount(int count) {
    this.count = count;
  }
}
